package com.su.mw.mscthesis.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jbpm.api.TaskService;

import com.su.mw.mscthesis.util.MScThesisUtils;

/**
 * Servlet implementation class SubmitThesisServlet
 */
public class SubmitThesisServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SubmitThesisServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Check that we have a file upload request
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		if (isMultipart) {
			// Create a factory for disk-based file items
			FileItemFactory factory = new DiskFileItemFactory();

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

			// Parse the request
			try {
				List /* FileItem */items = upload.parseRequest(request);

				String department = null;
				String faculty = null;
				String taskId = null;
				byte[] data = null;
				String fileName = null;
				// Process the uploaded items
				Iterator iter = items.iterator();
				while (iter.hasNext()) {
					FileItem item = (FileItem) iter.next();

					if (item.isFormField()) {
						String name = item.getFieldName();
						String value = item.getString();
						System.out.println(name + ", "  + value);
						if ("department".equals(name))
							department = value;
						else if ("faculty".equals(name))
							faculty = value;
						else if ("taskId".equals(name))
							taskId = value;
					} else {
						String fieldName = item.getFieldName();
						fileName = item.getName();
						String contentType = item.getContentType();
						long sizeInBytes = item.getSize();
						data = item.get();
						System.out.println(fileName + " , " + sizeInBytes);
					}
				}

				submitThesis(taskId, department, faculty,data, fileName);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}


		}
	}


	protected void submitThesis(String taskId, String department, String faculty, byte[] file, String fileName){
		TaskService ts = MScThesisUtils.PROCESS_ENGINE.getTaskService();
		Map<String, Object> variables = new HashMap<String, Object> ();
		variables.put("department", department);
		variables.put("faculty", faculty);
		variables.put("document", file);
		variables.put("fileName", fileName);
		Calendar calendar = Calendar.getInstance();
		calendar.roll(Calendar.DAY_OF_MONTH, 20);
		variables.put("graduationDate", calendar.getTime());
		ts.setVariables(taskId, variables);
		ts.completeTask(taskId, "thesis completed");
	}

}
